package cn1shared.util;


import java.util.Collection;

public class CollectionUtil {

	private CollectionUtil() {} // never

	public static void populate(String csv, char separator, Collection<String> collection) {
		collection.clear();
		if (csv == null)
			return;
		int pos = csv.indexOf(separator);
		if (pos < 0) {
			collection.add(csv);
			return;
		}
		int begin = 0;
		while (pos >= 0) {
			String v = csv.substring(begin, pos);
			collection.add(v);
			begin = pos + 1;
			pos = csv.indexOf(separator, begin);
		}
		collection.add(csv.substring(begin));
	}

	public static String asString(Collection<String> collection, char separator) {
		StringBuilder out = new StringBuilder();
		for (String s : collection) {
			if (out.length() > 0)
				out.append(separator);
			out.append(s);
		}
		return out.toString();
	}

}
